﻿using System;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using SuntabuAssistant.AssistAchieve;
using Application = System.Windows.Application;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using MessageBox = System.Windows.Forms.MessageBox;

namespace SuntabuAssistant.Interface
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login : Window
    {
        private static Login login = null;
        private MainWindow _mw;
        public Login()
        {

            InitializeComponent();
            ResizeMode = ResizeMode.NoResize;
            passwordBox1.Focus();//聚焦在用户名输入框中
            Assistant.Instance.Hide += HideWin;
            _mw = MainWindow.Instance;
            //  this.statusBar1.Items.Add(new Label());
        }



        private void button1_Click(object sender, RoutedEventArgs e)
        {
            var enterStr = this.passwordBox1.Password;
            if (enterStr == String.Empty)
            {
                this.label2.Content = "enter something !";
            }
            else if (enterStr == SuntabuAssistant.Properties.Resources.key1)
            {
                ShowMw();
                var assistant = Assistant.Instance;
                assistant.IsZhun = true;
            }
            else
            {
                this.label2.Content = "Wrong key,try again";
            }
        }

        private void passwordBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                button1_Click(sender, e);
            }

        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            ShowMw();
        }

        private void ShowMw()
        {
            var mw = MainWindow.Instance;
            mw.Show();

            this.Hide();
        }

        private void button2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                button2_Click(sender, e);
            }
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
        }

        public void HideWin()
        {
            this.Hide();
        }

        public void HideWin(Window win)
        {
            win.Hide();
        }

        public void HideWin(int times)
        {
            if (times % 10 == 0)
            {
                this.Hide();
                //var mw = MainWindow.Instance;
                //mw.Hide();
            }
            else
            {
                label3.Content = (10 - times % 10).ToString() + "s left To Hide";
            }
        }

        public static Login Instance
        {
            get { return login ?? (login = new Login()); }
        }
    }
}
